package com.facebank.usersupport.aop;

import com.facebank.usersupport.mapper.usersupport.usersupport.VisitsMapper;
import com.facebank.usersupport.model.Activity;
import com.facebank.usersupport.model.RestModel;
import com.facebank.usersupport.model.Visits;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class VisitsAspect {

    @Autowired
    private VisitsMapper visitsMapper;

    @AfterReturning(returning = "rvt",pointcut = "execution(* com.facebank.usersupport.service.impl.JsonpServiceImpl.detail(..))")
    public void record(Object rvt){
        RestModel restModel = (RestModel) rvt;
        Activity activity = (Activity) restModel.getData();
        Visits visits = visitsMapper.selectByActivityId(activity.getId());
        if (null != visits){
            visits.setNumber(visits.getNumber() + 1);
            visits.setUpdateTime(System.currentTimeMillis());
            visitsMapper.updateByPrimaryKeySelective(visits);
        }else {
            Visits visits1 =new Visits();
            visits1.setNumber(1L);
            visits1.setActivityId(activity.getId());
            visits1.setUpdateTime(System.currentTimeMillis());
            visitsMapper.insertSelective(visits1);
        }
    }
}